
#include "pretreatment.h"

void pretreatment::gamma(cv::Mat & for_gamma , cv::Mat & result, float gamma_k)
{
	uchar LUT[256];
	for_gamma.copyTo(result);
	for (int i = 0; i < 256; i++){
		//float f = (i + 0.5f) / 255;
		float f = i  / 255.0;
		f = pow(f, gamma_k);
		//LUT[i] = cv::saturate_cast<uchar>(f*255.0f-0.5f);
		LUT[i] = cv::saturate_cast<uchar>(f*255.0);
	}
	
	if (result.channels() == 1){
		cv::MatIterator_<uchar> it = result.begin<uchar>();
		cv::MatIterator_<uchar> it_end = result.end<uchar>();
		for (; it != it_end; ++it){
			*it = LUT[(*it)];
		}
	}
	else{
		cv::MatIterator_<cv::Vec3b> it = result.begin<cv::Vec3b>();
		cv::MatIterator_<cv::Vec3b> it_end = result.end<cv::Vec3b>();
		for (; it != it_end; ++it){
			(*it)[0] = LUT[(*it)[0]];
			(*it)[1] = LUT[(*it)[1]];
			(*it)[2] = LUT[(*it)[2]];
		}
	}
}

void pretreatment::sharp(cv::Mat & for_sharp , cv::Mat & result)
{
    cv::Mat kernel(3,3,CV_32F,cv::Scalar(0));
    kernel.at<float>(1,1) = 5.0;
    kernel.at<float>(0,1) = -1.0;
    kernel.at<float>(1,0) = -1.0;
    kernel.at<float>(1,2) = -1.0;
    kernel.at<float>(2,1) = -1.0;
    result.create(for_sharp.size(),for_sharp.type());    
    //对图像进行滤波
    cv::filter2D(for_sharp,result,for_sharp.depth(),kernel);
}

pretreatment::pretreatment()
{

}

pretreatment::~pretreatment()
{

}